package codeGeneration.persistent;

import java.io.FileWriter;
import java.io.IOException;

import util.HeaderOfJavaFile;

import codeGeneration.Generator;
import databaseTransaction.Table;

public abstract class PersistentGenerator extends Generator {
	protected String className;
	protected Table table;
	protected StringBuffer persistentBuffWriter;
	protected StringBuffer daoBuffWriter;
	protected String persistentPackageName;
	protected String daoPackageName;
	protected String persistentFileLocation;
	protected String daoFileLocation;
	protected String defaultObjName;
	protected HeaderOfJavaFile daoHeaderOfJavaFile;
	protected HeaderOfJavaFile persistentHeaderOfJavaFile;

	public PersistentGenerator(Table table, String className, String packageName) {
		this.table = table;
		this.className = className;
		this.defaultObjName = "" + ((char) (className.charAt(0) + 32))
				+ className.substring(1, className.length());
		this.persistentPackageName = packageName;
		this.daoPackageName = packageName.replace("persistent", "dao");
		this.daoPackageName = daoPackageName.substring(0,
				daoPackageName.lastIndexOf('.'));

		// ---------------------------------------------------------------

		persistentBuffWriter = new StringBuffer();
		persistentHeaderOfJavaFile = new HeaderOfJavaFile();
		indentOneTime(HeaderOfJavaFile.packageNamePlaceHolder, null, ";\n",
				persistentBuffWriter, true);
		indentOneTime(HeaderOfJavaFile.importsListPlaceHolder, null, "",
				persistentBuffWriter, true);

		// ---------------------------------------------------------------

		daoBuffWriter = new StringBuffer();
		daoHeaderOfJavaFile = new HeaderOfJavaFile();
		indentOneTime(HeaderOfJavaFile.packageNamePlaceHolder, null, ";\n",
				daoBuffWriter, true);
		indentOneTime(HeaderOfJavaFile.importsListPlaceHolder, null, "",
				daoBuffWriter, true);
	}

	public void setClassName(String className) {
		this.className = className;
	}

	public String getClassName() {
		return this.className;
	}

	public void setTable(Table table) {
		this.table = table;
	}

	public Table getTable() {
		return this.table;
	}

	public void generatePersistent() throws IOException {
		generateDeclare();
		generateGetAll();
		generateGetById();
		generateGetAndSetBy();
		generateAdd();
		generateRemove();
		generateRemoveAll();
		decreaseIndentOneTime("}", daoBuffWriter);
		appendToFile(persistentBuffWriter, "}\n");

		replacePlaceHolder(persistentBuffWriter, persistentHeaderOfJavaFile);
		replacePlaceHolder(daoBuffWriter, daoHeaderOfJavaFile);

		FileWriter persistentFileWriter = null;
		FileWriter daoFileWriter = null;

		try {

			persistentFileWriter = getFileWriter(persistentFileLocation + '/'
					+ this.className + "Persistent.java");
			daoFileWriter = getFileWriter(daoFileLocation + '/'
					+ this.className + "DAO.java");

			persistentFileWriter.append(persistentBuffWriter.toString());
			daoFileWriter.append(daoBuffWriter.toString());
		} catch (IOException e) {
			e.printStackTrace();
			throw e;
		}

		if (persistentFileWriter != null)
			closeFileWriter(persistentFileWriter);
		if (daoFileWriter != null)
			closeFileWriter(daoFileWriter);
	}

	protected abstract void generateDeclare();

	protected abstract void generateGetAll();

	protected abstract void generateGetById();

	protected abstract void generateGetAndSetBy();

	protected abstract void generateAdd();

	protected abstract void generateRemove();

	protected abstract void generateRemoveAll();
}
